var express = require('express');
var router = express.Router();
const orderService = require('../service/orderService');

/* 获取订单数据 */
router.get('/getorder', async function (req, res) {
   let { current, pageSize, type, value } = req.query;
   let searchParams = {};
   if (type) {
      if (type == "storeName") {
         searchParams[type] = value;
      } else {
         searchParams[type] = { $regex: value };
      }
   }
   let data = await orderService.find({ current: current || 1, pageSize: pageSize || 5 }, searchParams);
   res.send(data);
});


// id查找
router.get('/findbyid', async function (req, res) {
   let { id } = req.query;
   let data = await orderService.findById(id);
   res.send(data);
});


// 添加
router.post('/insert', async function (req, res) {
   let { storeName, storePhone, km, total, payment, mode, time, notes, address, state, orderNum, spare, userId, name, poster, specs } = req.body;
   console.log(storeName, storePhone, km, total, payment, mode, time, notes, address, state, orderNum, spare, userId, name, poster, specs);
   let data = await orderService.insert({ storeName, storePhone, km, total, payment, mode, time, notes, address, state, orderNum, spare, userId, name, poster, specs });
   res.send(data);
});


//删除
router.delete('/remove', async function (req, res) {
   let { id } = req.body;
   console.log(id);
   let data = await orderService.delete(id);
   res.send(data);
});

// 修改
router.put('/update', async function (req, res) {
   let { _id, storeName, storePhone, km, total, payment, mode, time, notes, address, state, orderNum, spare, userId, name, poster, specs } = req.body;
   console.log(_id, storeName, storePhone, km, total, payment, mode, time, notes, address, state, orderNum, spare, userId, name, poster, specs)
   let data = await orderService.update({ _id, storeName, storePhone, km, total, payment, mode, time, notes, address, state, orderNum, spare, userId, name, poster, specs });
   res.send(data);
});



router.get('/ordertotal', async function (req, res) {

   let datas = await orderService.findAll();
   res.send(datas);
});



module.exports = router;
